﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface.Presentation.Controls;

namespace LetsGoToSchool
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : UserControl
    {

        private Main _main;
        private String _newLogin;
        private String _currUser;
        
        public Login()
        {
            InitializeComponent();

            List<string> accounts = Database.getAllAccounts();
            
            for (int i = 0; i < accounts.Count; i++)
            {
                // make an item out of accounts[i] and add it to the surface list box
                
                SurfaceListBoxItem item = new SurfaceListBoxItem();
                item.Content = accounts[i];
                item.Background = Brushes.LightSkyBlue;
                item.FontSize = 30;
                item.FontWeight = FontWeights.Normal;
                item.Width = 350;
                item.HorizontalAlignment = HorizontalAlignment.Center;
                item.VerticalAlignment = VerticalAlignment.Top;
                item.HorizontalContentAlignment = HorizontalAlignment.Center;

                _validLogins.Items.Add(item);
            }

        }

        /// <summary>
        /// Sets the main screen object to the one passed in
        /// </summary>
        /// <param name="m"></param>
        public void setMain(Main m)
        {
            _main = m;
        }

        /// <summary>
        /// Called when the text in the login box is changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param
        private void _loginBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            _noTextWarning.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// Called when the keyboard has lost focus - for testing purposes only
        /// </summary>
        /// <param name="e"></param
        protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            _newLogin = _loginBox.Text;
        }

        /// <summary>
        /// Called when the start button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param
        private void _startButton_Click(object sender, RoutedEventArgs e)
        {
            if (_validLogins.SelectedIndex != -1)
            {
                _main.display(); // displays elements of the Main Frame that need to be visible
                this.Visibility = Visibility.Hidden;
                _main.Visibility = Visibility.Visible;

                // set the current user
                if (_currUser != null)
                {
                    _main.setCurrentUser(_currUser);
                }
            }
            else
            {
                _noLoginWarning.Visibility = Visibility.Visible;
            }
        }

        private void _newUserButton_Click(object sender, RoutedEventArgs e)
        {
            new_login.Visibility = Visibility.Visible;
            existing_logins.Visibility = Visibility.Hidden;
        }

        private void _addAccountButton_Click(object sender, RoutedEventArgs e)
        {
            _newLogin = _loginBox.Text;

            if (_newLogin.Equals(""))
            {
                _noTextWarning.Visibility = Visibility.Visible;
            }
            else
            {
                // add it to the list of usernames displayed on existing logins
                SurfaceListBoxItem item = new SurfaceListBoxItem();
                item.Content = _loginBox.Text;
                item.Background = Brushes.LightSkyBlue;
                item.FontSize = 30;
                item.FontWeight = FontWeights.Normal;
                item.Width = 350;
                item.HorizontalAlignment = HorizontalAlignment.Center;
                item.VerticalAlignment = VerticalAlignment.Top;
                item.HorizontalContentAlignment = HorizontalAlignment.Center;

                _validLogins.Items.Add(item);
                new_login.Visibility = Visibility.Hidden;
                existing_logins.Visibility = Visibility.Visible;

                Database.createAccount(_loginBox.Text);

                _loginBox.Text = ""; // set the text to empty again
            }
            

        }

        private void _cancelButton_Click(object sender, RoutedEventArgs e)
        {
            _noTextWarning.Visibility = Visibility.Hidden;
            new_login.Visibility = Visibility.Hidden;
            existing_logins.Visibility = Visibility.Visible;

            _loginBox.Text = ""; // set the text to empty again
        }

        private void _removeUserButton_Click(object sender, RoutedEventArgs e)
        {
            // obtain the selected username from the list and remove it from the
            // displayed list as well as from the database

            Database.removeAccount(((SurfaceListBoxItem)(_validLogins.SelectedItem)).Content.ToString());
            _validLogins.Items.Remove(_validLogins.SelectedItem);

        }

        private void _validLogins_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_validLogins.SelectedIndex != -1) // if something is selected
            {

                if (_validLogins.Items.Count == 0) // if it's empty
                {
                    // if you want colors to change - or new user button to have highlighted border
                   
                }
                _currUser = ((SurfaceListBoxItem)(_validLogins.SelectedItem)).Content.ToString();
                _removeUserButton.Visibility = Visibility.Visible;
                _noLoginWarning.Visibility = Visibility.Hidden;
            }
            else
            {
                _removeUserButton.Visibility = Visibility.Hidden;

            }
        }
    }
}
